package com.vue.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vue.pojo.Order_Demo;
import com.vue.service.Order_De_Service;
import com.vue.service.PayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalTime;

@RestController
@RequestMapping("/order")
public class Order_DemoController {
    @Autowired
    private Order_De_Service order_de_service;
    @Autowired
    private PayService payService;

    @RequestMapping("/show")
    public ResponseEntity<Page<Order_Demo>> finadll(Integer pagenumber, Integer pagesize) {
        Page<Order_Demo> findll = order_de_service.findll(pagenumber, pagesize);
        for (int i = 0; i < findll.getRecords().size(); i++) {
            findll.getRecords().get(i).setOrderStatus(payService.findById(Integer.parseInt(findll.getRecords().get(i).getOrderStatus())).getName());
        }
        return new ResponseEntity<>(findll, HttpStatus.OK);
    }

    @RequestMapping("/del")
    public ResponseEntity<String> delOne(String id) {
        int i = 9;
        Order_Demo one = order_de_service.findOne(Integer.parseInt(id));
        System.out.println(payService.findById(Integer.parseInt(one.getOrderStatus())).getName());
        if (payService.findById(Integer.parseInt(one.getOrderStatus())).getName().equals("已收货")) {
            i = order_de_service.delOne(Integer.parseInt(id));
            if (i == 1) {
                return new ResponseEntity<>("删除成功", HttpStatus.OK);
            } else {
                return new ResponseEntity<>("删除失败", HttpStatus.OK);
            }
        } else {
            return new ResponseEntity<>("删除失败", HttpStatus.OK);
        }

    }

    @RequestMapping("/fa")
    public ResponseEntity<String> da(Integer id) {
        System.out.println(id);
        Order_Demo one = order_de_service.findOne(id);
        if (payService.findById(Integer.parseInt(one.getOrderStatus())).getName().equals("已支付")) {
            one.setOrderStatus(payService.fingByName("已发货").getId() + "");
            one.setSendTime(LocalDate.now() + " " + LocalTime.now().getHour() + ":" + LocalTime.now().getMinute() + ":" + LocalTime.now().getSecond());
            order_de_service.upOne(one);
            return new ResponseEntity<>("已经发货", HttpStatus.OK);
        } else {
            return new ResponseEntity<>("未付款", HttpStatus.OK);
        }
    }

    @RequestMapping("/sel")
    public ResponseEntity<Page<Order_Demo>> sel(@RequestParam(required = false) Integer id, @RequestParam(required = false) String name, Integer pagenumber, Integer pagesize) {
        System.out.println(id + name+"--------");
        if (!StringUtils.isEmpty(name)) {
            switch (Integer.parseInt(name)) {
                case 1:
                    name= payService.fingByName("未支付").getId()+"";
                    break;
                case 2:
                    name=payService.fingByName("已支付").getId()+"";
                    break;
                case 3:
                    name=payService.fingByName("已发货").getId()+"";
                    break;
                case 4:
                    name=payService.fingByName("已收货").getId()+"";
                    break;
                case 5:
                    name=payService.fingByName("已评价").getId()+"";
                    break;
            }

        }
        Page<Order_Demo> findlll = order_de_service.findlll(pagenumber, pagesize, id, name);
        for (int i = 0; i < findlll.getRecords().size(); i++) {
            findlll.getRecords().get(i).setOrderStatus(payService.findById(Integer.parseInt(findlll.getRecords().get(i).getOrderStatus())).getName());
        }
        System.out.println(findlll.getRecords());
        return new ResponseEntity<>(findlll, HttpStatus.OK);
    }

}
